package com.chushouya.manager.dao.repository;

import com.general.framework.core.enums.DelFlagEnum;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.PartnerPromoteEntity;
import com.chushouya.manager.dao.mapper.PartnerPromoteEntityMapper;
import com.chushouya.manager.dto.admin.partner.PartnerPromoteQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class PartnerPromoteRepository extends BaseRepository<PartnerPromoteEntityMapper, PartnerPromoteEntity, Long> {

    public PageResult<PartnerPromoteEntity> selectPageResult(PartnerPromoteQuery query) {
        Example example = this.buildExample(query);
        example.orderBy("createTime").desc();
        return this.selectPage(query.getPageRequest(), example);
    }

    private Example buildExample(PartnerPromoteQuery query) {
        final Example example = this.buildExample();
        if (Objects.nonNull(query.getPromoteId())) {
            example.and().andEqualTo("promoteId", query.getPromoteId());
        }
        if (Strings.isNotEmpty(query.getPromoteLabel())) {
            example.and().andEqualTo("promoteLabel", query.getPromoteLabel());
        }
        if (Strings.isNotEmpty(query.getPromoteChannel())) {
            example.and().andEqualTo("promoteChannel", query.getPromoteChannel());
        }
        if (Objects.nonNull(query.getPartnerId())) {
            example.and().andEqualTo("partnerId", query.getPartnerId());
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        example.and().andEqualTo("delFlag", DelFlagEnum.normal.getValue());
        return example;
    }

    public PartnerPromoteEntity selectOne(PartnerPromoteQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }
}
